/*
 * Copyright (c) 2021 Huawei Device Co.,  Ltd.
 * Licensed under the Apache License,  Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,  software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,  either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FINANCE_RECPTPRINTER_SRV_CLIENT_H
#define FINANCE_RECPTPRINTER_SRV_CLIENT_H

#include <singleton.h>
#include <memory>
#include <mutex>
#include "iremote_object.h"
#include "irecptprinter_srv.h"
#include "cashdevice_client.h"
#include "cashdevice_proxy.h"

namespace SwanLink {
namespace Finance {
class RecptprinterSrvClient final : public DelayedRefSingleton<RecptprinterSrvClient>, public CashDeviceClient {
    DECLARE_DELAYED_REF_SINGLETON(RecptprinterSrvClient)

 public:
    DISALLOW_COPY_AND_MOVE(RecptprinterSrvClient);

    /**
     * 获取打印格式
     */
    int GetPrintFormat(int handle, PrintFormatType ftt);
    /**
     * 设置打印格式
     */
    void SetPrintFormat(int handle, PrintFormatType ftt, int value);
    /**
     * 获取设备配置
     */
    void GetDeviceSpec(int handle, DeviceSpec &spec);
    /**
     * 走纸
     */
    int FeedPaper(int handle, int line);
    /**
     * 切纸
     */
    int CutPaper(int handle, int line, int mode);
    /**
     * 添加文本
     */
    int AddText(int handle, int x, int y, const std::string& text);
    /**
     * 添加图片
     */
    int AddBitmap(int handle, int x, int y, const std::string& lpath);
    /**
     * 添加二维码
     */
    int AddQRCode(int handle, int x, int y, int w, const std::string& qr);
    /**
     * 添加条码
     */
    int AddBarCode(int handle, int x, int y, BarCodeSpec &spec, const std::string& bar);
    /**
     * 开始打印
     */
    void StartPrint(int handle, int timeout, PrintListener *listener);

private:
    class RecptprinterSrvDeathRecipient : public IRemoteObject::DeathRecipient {
    public:
        RecptprinterSrvDeathRecipient() = default;
        ~RecptprinterSrvDeathRecipient() = default;
        void OnRemoteDied(const wptr<IRemoteObject>& remote);
    private:
        DISALLOW_COPY_AND_MOVE(RecptprinterSrvDeathRecipient);
    };

    ErrCode Connect();
    sptr<ICashDeviceSrv> Proxy() {return proxy_;};

    sptr<IRecptprinterSrv> proxy_ {nullptr};
    sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr};

    void ResetProxy(const wptr<IRemoteObject>& remote);
    std::mutex mutex_;
};
}  // namespace Finance
}  // namespace SwanLink

#endif // FINANCE_RECPTPRINTER_SRV_CLIENT_H
